package com.dtw.ditouwang.ui.more;

import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.dtw.ditouwang.R;
import com.dtw.ditouwang.ui.OnItemClickListener;
import com.dtw.ditouwang.ui.home.CropSquareTransformation;

import java.util.List;

public class SendCommentImageAdapter extends RecyclerView.Adapter<SendCommentImageAdapter.Holder> {
    private List<Bitmap> bitmapList;
    private OnItemClickListener onItemClickListener;
    CropSquareTransformation cropSquareTransformation = new CropSquareTransformation();
    RequestOptions options = RequestOptions.bitmapTransform(cropSquareTransformation);

    public SendCommentImageAdapter(List<Bitmap> bitmapList) {

        this.bitmapList = bitmapList;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new Holder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_square_imageview, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int i) {
        if(i==bitmapList.size()){
//            holder.imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
//            holder.imageView.setImageResource(R.drawable.ic_camera);
            Glide.with(holder.imageView).load(R.drawable.icon_camera).apply(options).into(holder.imageView);
//            Glide.with(holder.imageView).load(ContextCompat.getDrawable(holder.imageView.getContext(),R.drawable.icon_camera)).apply(options).into(holder.imageView);
        }else {
//            holder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            Glide.with(holder.imageView).load(bitmapList.get(i)).apply(options).into(holder.imageView);
        }
    }

    @Override
    public int getItemCount() {
        return bitmapList.size()+1;
    }

    class Holder extends RecyclerView.ViewHolder {
        ImageView imageView;

        public Holder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image_image);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onItemClickListener!=null){
                        onItemClickListener.onItemClick(0,getAdapterPosition(),0);
                    }
                }
            });
        }
    }
}